package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class StemealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {

        this.save(setmealDto);

        Long setmealId = setmealDto.getId();

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

    }

    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {

        LambdaQueryWrapper<Setmeal> queryWrapper_stemeal = new LambdaQueryWrapper<>();

        queryWrapper_stemeal.in(Setmeal::getId,ids)
                .eq(Setmeal::getStatus,1);

        int count = this.count(queryWrapper_stemeal);
        if (count==0){
            this.deleteByIds(ids);
        }else {
            throw new CustomException("套餐正在售卖，不可删除");
        }

        LambdaQueryWrapper<SetmealDish> queryWrapper_setmealDish = new LambdaQueryWrapper<>();
        queryWrapper_setmealDish.in(SetmealDish::getSetmealId,ids);

        setmealDishService.remove(queryWrapper_setmealDish);

    }

    @Override
    public SetmealDto getByDish(Long id) {

        SetmealDto setmealDto = new SetmealDto();

        Setmeal setmeal = this.getById(id);

        BeanUtils.copyProperties(setmeal,setmealDto);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(SetmealDish::getSetmealId,id);

        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    @Override
    public void updateByDish(SetmealDto setmealDto) {

        this.updateById(setmealDto);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());

        setmealDishService.remove(queryWrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes().stream().map((item) -> {

            item.setSetmealId(setmealDto.getId());

            return item;

        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

    }
}
